﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
using GenericEntities;
using System.Collections;

namespace DiaryPlanner
{
    public partial class PlannerMonth : UserControl
    {
        private Planner planner;

        public GenericEvent genericEvent;

        // Sets a DateTime to April 3, 2002 of the Gregorian calendar.
        public DateTime myDT = DateTime.Now;

        // Uses the default calendar of the InvariantCulture.
        public Calendar myCal = CultureInfo.InvariantCulture.Calendar;

        List<Label> monthList = new List<Label>();

        System.Drawing.Color userColor = System.Drawing.Color.SkyBlue;
        System.Drawing.Color userColor1 = System.Drawing.Color.GreenYellow;
        System.Drawing.Color backColor;

        public PlannerMonth()
        {
            InitializeComponent();
          
            initMonth();
        }
       
        private void initMonth()
        {
            CheckForIllegalCrossThreadCalls = false; //스레드 충돌에러 없애기

            // 모든 월들을 MonthList에 저장
            monthList.Add(null);
            monthList.Add(monthL1);
            monthList.Add(monthL2);
            monthList.Add(monthL3);
            monthList.Add(monthL4);
            monthList.Add(monthL5);
            monthList.Add(monthL6);
            monthList.Add(monthL7);
            monthList.Add(monthL8);
            monthList.Add(monthL9);
            monthList.Add(monthL10);
            monthList.Add(monthL11);
            monthList.Add(monthL12);
            monthList.Add(monthL13);
            monthList.Add(monthL14);
            monthList.Add(monthL15);
            monthList.Add(monthL16);
            monthList.Add(monthL17);
            monthList.Add(monthL18);
            monthList.Add(monthL19);
            monthList.Add(monthL20);
            monthList.Add(monthL21);
            monthList.Add(monthL22);
            monthList.Add(monthL23);
            monthList.Add(monthL24);
            monthList.Add(monthL25);
            monthList.Add(monthL26);
            monthList.Add(monthL27);
            monthList.Add(monthL28);
            monthList.Add(monthL29);
            monthList.Add(monthL30);
            monthList.Add(monthL31);
            monthList.Add(monthL32);
            monthList.Add(monthL33);
            monthList.Add(monthL34);
            monthList.Add(monthL35);
            monthList.Add(monthL36);
            monthList.Add(monthL37);
            monthList.Add(monthL38);
            monthList.Add(monthL39);
            monthList.Add(monthL40);
            monthList.Add(monthL41);
            monthList.Add(monthL42);

            //클릭 이벤트 핸들러 등록
            for (int i = 1; i < monthList.Count; i++)
                monthList[i].Click += new System.EventHandler(this.month_Click);

            //더블 클릭 이벤트 핸들러 등록
            for (int i = 1; i < monthList.Count; i++)
                monthList[i].DoubleClick += new System.EventHandler(this.month_DoubleClick);

            backColor = monthList[1].BackColor;
            
            //해당 월에 일값을 설정
            viewAll();
        }
        
        public void viewAll()
        {
            for (int i = 1; i < monthList.Count; i++)
                monthList[i].Text = "";

            int lastDay = getLastDay();
            int firstweek = getFirstWeek();

            for (int i = 1; i <= lastDay; i++)
            {
                monthList[i + firstweek].Text = i.ToString();
            }
            if (planner != null) viewAllDB();
        }
         
        public void viewAllDB()
        {
            IList list = CalendarDB.Instance().loadAll_db();

            int lastDay = getLastDay();
            int firstweek = getFirstWeek();

            planner.plannerListClear();

            int select = 0;

            for (int i = 1; i < monthList.Count; i++)
            {
                if (monthList[i].BackColor == userColor)
                {
                    select = i - firstweek;
                }
                else if (monthList[i].BackColor == userColor1)
                {
                    monthList[i].BackColor = backColor;
                }
            }

            for (int j = firstweek; j <= lastDay + firstweek; j++)
            {
                for (int i = 0; i < list.Count; i++)
                {
                    if (monthList[j].Text.IndexOf('\n') > 0)
                    {
                        if (planner.monthCalendar1.SelectionStart.Year.ToString() == ((GenericEvent)list[i]).StartTime.Year.ToString() &&
                        planner.monthCalendar1.SelectionStart.Month.ToString() == ((GenericEvent)list[i]).StartTime.Month.ToString() &&
                        monthList[j].Text.Substring(0, monthList[j].Text.IndexOf('\n')) == ((GenericEvent)list[i]).StartTime.Day.ToString())
                        {
                            if (((GenericEvent)list[i]).Title != null && monthList[j].BackColor == userColor) ;
                            else if (((GenericEvent)list[i]).Title != null) monthList[j].BackColor = userColor1;
                            else monthList[j].BackColor = backColor;

                            if (((GenericEvent)list[i]).Title.Length > 5)
                                monthList[j].Text = monthList[j].Text + "\n" + ((GenericEvent)list[i]).Title.Substring(0, 6);
                            else
                                monthList[j].Text = monthList[j].Text + "\n" + ((GenericEvent)list[i]).Title;

                            if (select == int.Parse(monthList[j].Text.Substring(0, monthList[j].Text.IndexOf('\n'))))
                                planner.plannerListAdd((GenericEvent)list[i]);
                        }
                    }
                    else
                    {
                        if (planner.monthCalendar1.SelectionStart.Year.ToString() == ((GenericEvent)list[i]).StartTime.Year.ToString() &&
                        planner.monthCalendar1.SelectionStart.Month.ToString() == ((GenericEvent)list[i]).StartTime.Month.ToString() &&
                        monthList[j].Text == ((GenericEvent)list[i]).StartTime.Day.ToString())
                        {
                            if (((GenericEvent)list[i]).Title != null && monthList[j].BackColor == userColor) ;
                            else if (((GenericEvent)list[i]).Title != null) monthList[j].BackColor = userColor1;
                            else monthList[j].BackColor = backColor;

                            if (select == int.Parse(monthList[j].Text))
                                planner.plannerListAdd((GenericEvent)list[i]);

                            if (((GenericEvent)list[i]).Title.Length > 5)
                                monthList[j].Text = monthList[j].Text + "\n" + ((GenericEvent)list[i]).Title.Substring(0, 6);
                            else
                                monthList[j].Text = monthList[j].Text + "\n" + ((GenericEvent)list[i]).Title;
                        }
                    }
                }
            }
        }

        public void updateMonthDB()
        {
            genericEvent = new GenericEvent();

            genericEvent.StartTime = planner.monthCalendar1.SelectionStart;
        }
        
        private int getLastDay()
        {
            int lastDay, tempMonth;
            DateTime tempDT = myDT;

            tempMonth = tempDT.Month;

            for (int i = 0; ; i++)
            {
                tempDT = myCal.AddDays(tempDT, 1);
                if (tempMonth != tempDT.Month)
                    break;
            }
            tempDT = myCal.AddDays(tempDT, -1);
            lastDay = tempDT.Day;

            return lastDay;
        }

        public int getFirstWeek()
        {
            int firstWeek, tempMonth;
            DateTime tempDT = myDT;

            tempMonth = tempDT.Month;

            for (int i = 0; ; i++)
            {
                tempDT = myCal.AddDays(tempDT, -1);
                if (tempMonth != tempDT.Month)
                    break;
            }
            tempDT = myCal.AddDays(tempDT, +1);

            firstWeek = (int)tempDT.DayOfWeek;

            return firstWeek;
        }

        private void month_Click(object sender, EventArgs e)
        {
            Label label = (Label)sender;

            if (label.Text == "") return;

            for (int i = 1; i < monthList.Count; i++)
                if(monthList[i].BackColor == userColor)
                    monthList[i].BackColor = backColor;

            label.BackColor = userColor;

            if (label.Text.IndexOf('\n') > 0)
            {
                planner.monthCalendar1.SelectionStart = planner.monthCalendar1.SelectionStart.AddDays
                                                        (int.Parse(label.Text.Substring(0, label.Text.IndexOf('\n'))) - planner.monthCalendar1.SelectionStart.Day);
                planner.monthCalendar1.SelectionEnd = planner.monthCalendar1.SelectionStart.AddDays
                                                        (int.Parse(label.Text.Substring(0, label.Text.IndexOf('\n'))) - planner.monthCalendar1.SelectionStart.Day);
            }
            else
            {
                planner.monthCalendar1.SelectionStart = planner.monthCalendar1.SelectionStart.AddDays
                                                        (int.Parse(label.Text) - planner.monthCalendar1.SelectionStart.Day);
                planner.monthCalendar1.SelectionEnd = planner.monthCalendar1.SelectionStart.AddDays
                                                        (int.Parse(label.Text) - planner.monthCalendar1.SelectionStart.Day);
            }
        }

        private void month_DoubleClick(object sender, EventArgs e)
        {
            Label label = (Label)sender;

            if (label.Text == "") return;

            updateMonthDB();

            PlannerEdit PlannerEditObject = new PlannerEdit();
            PlannerEditObject.initPlannerEdit(planner,this);
            PlannerEditObject.ShowDialog();
            viewAll();
        }

        public void month_Click(object sender)
        {
            month_Click(sender, null);
        }

        public void month_DoubleClick(object sender)
        {
            month_DoubleClick(sender, null);
        }

        public Label getMonthList(int index)
        {
            return monthList[index];
        }

        public void initPlanner(Planner planner)
        {
            this.planner = planner;
        }
        
        public void setUnSelected()
        {
            for (int i = 1; i < monthList.Count; i++)
                if (monthList[i].BackColor == userColor)
                    monthList[i].BackColor = backColor;
        }
        
    }
}
